package com.burgeon.backend.SHOPBACKEND.controller;

import com.alibaba.fastjson.JSON;
import com.burgeon.backend.SHOPBACKEND.entity.Classification;
import com.burgeon.backend.SHOPBACKEND.entity.Dimdef;
import com.burgeon.backend.SHOPBACKEND.entity.Goods;
import com.burgeon.backend.SHOPBACKEND.service.BackendGoodsService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("shopbackent/goods")
public class BackentGoodsController extends BaseController {

    @Autowired
    private BackendGoodsService backendGoodsService;


    //上传商品视频
    @RequestMapping("uploadProductVideo")
    public ResponseResult<String> uploadProductVideo(@RequestParam(value = "video", required = false)  MultipartFile video,String account) {
        String data = backendGoodsService.uploadProductVideo(video, account);

        return new ResponseResult<String>(SUCCESS,data);
    }


    //取消包邮
    @RequestMapping("cancelPackageMailed")
    public ResponseResult<Void> cancelPackageMailed(String gids, String account) {

        backendGoodsService.cancelPackageMailed(gids,account);
        return new ResponseResult<>(SUCCESS);
    }

    //包邮列表
    @RequestMapping("setexemptionmaillist")
    public ResponseResult<List<Goods>> getExemptionMailList(String account) {

        List<Goods> data = backendGoodsService.getExemptionMailList(account);
        return new ResponseResult<List<Goods>>(SUCCESS,data);
    }


    //设置包邮
    @RequestMapping("setexemptionmail")
    public ResponseResult<Void> setExemptionMail(String gids, String account) {
        backendGoodsService.freeShipping(gids,account);

        return new ResponseResult<>(SUCCESS);
    }

    //批量修改商品自定义标签
    @RequestMapping("changealllabel")
    public ResponseResult<Void> changeAllLabel(String gids,String label) {
        String[] gidArr = gids
                .replace("\"","")
                .replace("]","")
                .replace("[","")
                .trim()
                .split(",");
        backendGoodsService.changeAllLabel(gidArr,label);
        return new ResponseResult<>(SUCCESS);
    }

    //批量修改商品销售区域
    @RequestMapping("changealltype")
    public ResponseResult<Void> changeAllType(String[] gids,String type){
        backendGoodsService.changeAllType(gids,type);
        return new ResponseResult<>(SUCCESS);
    }

    //批量修改商品活动方案
    @RequestMapping("changeallactivity")
    public ResponseResult<Void> changeAllActivity(String[] gids,String activityID,String activity){
        backendGoodsService.changeAllActivity(gids,activityID,activity);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("findgoodsclass")
    public ResponseResult<HashMap> findGoodsDataByClass(String name,String account) {
        HashMap data = backendGoodsService.findGoodsDataByClass(name,account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @RequestMapping("syncall")
    public ResponseResult<Void> syncAll(String type,String message,String message1,String account) {
        backendGoodsService.syncAll(type,message,message1,account);
        return new ResponseResult<Void>(SUCCESS);
    }

    @RequestMapping("findgoodscode")
    public ResponseResult<Goods> FindGoodsByCode(String code,String account) {
        Goods data = backendGoodsService.findGoodsDataByCode(code,account);
        return new ResponseResult<Goods>(SUCCESS, data);
    }

    @RequestMapping("getcachebyuse")
    public ResponseResult<List<Classification>> GetCache(String use,String account) {
        List<Classification> data = backendGoodsService.getGoodsCacheByUse(account,use);
        return new ResponseResult<List<Classification>>(SUCCESS, data);
    }

    @RequestMapping("getgoodsbyid")
    public ResponseResult<Map> GetGoodsById(String id) {
        Map data = backendGoodsService.getGoodsById(id);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    @RequestMapping("getcache")
    public ResponseResult<List<Classification>> GetCache(String account) {
        List<Classification> data = backendGoodsService.getGoodsCache(account);

        return new ResponseResult<List<Classification>>(SUCCESS, data);
    }

    @RequestMapping("addcache")
    public ResponseResult<Void> AddCache(@RequestParam("classification") String classification, String account) {
        System.err.println(classification);

        List<Classification> str = JSON.parseArray(classification, Classification.class);
        backendGoodsService.addGoodsCache(str, account);
        return new ResponseResult<Void>(SUCCESS);
    }

    @RequestMapping("findcode")
    public ResponseResult<Map> findGoodsByCode(String code, String account) {

        Map data = backendGoodsService.findGoodsByCode(code, account);
        return new ResponseResult<Map>(SUCCESS, data);
    }

    // 下架/上架 全部商品
    @RequestMapping("upstateall")
    public ResponseResult<Void> upGoodsStateAll(String[] ids) {
        backendGoodsService.upGoodsStateAll(ids);
        return new ResponseResult<Void>(SUCCESS);
    }

    @RequestMapping("upstate")
    public ResponseResult<Void> upGoodsState(String id, boolean state) {

        backendGoodsService.upGoodsState(id, state ? "Y" : "N");
        return new ResponseResult<Void>(SUCCESS);
    }

    @RequestMapping("deleteGoods")
    public ResponseResult<Void> DeleteGoods(String id, String account) {

        backendGoodsService.DeleteGoods(id, account);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("item")
    public ResponseResult<HashMap> GetItem(String id, String account) {
        HashMap data = backendGoodsService.GetProductalias(id, account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @PostMapping("updata")
    public ResponseResult<Void> UpGoods(String data,
                                        @RequestParam(value = "files", required = false) MultipartFile[] files,
                                        @RequestParam(value = "itemimg",required = false) MultipartFile[] itemimg,
                                        @RequestParam(value = "mfile") MultipartFile mfile,
                                        @RequestParam(value = "attribute",required = false)  MultipartFile attribute,
                                        String[] bid,String[] tid, String account) {

        System.err.println(mfile.getOriginalFilename());
        backendGoodsService.UpGoods(data, files, itemimg, mfile, attribute, account, bid,tid);
        return new ResponseResult<>(SUCCESS);
    }

    @PostMapping("sync")
    public ResponseResult<String> SynGoods(
            String data,
            @RequestParam(value = "files") MultipartFile[] files,
            @RequestParam(value = "itemimg") MultipartFile[] itemimg,
            @RequestParam(value = "mfile") MultipartFile mfile,
            @RequestParam(value = "attribute",required = false) MultipartFile attribute,
            String account) {

        System.err.println(itemimg[0].getOriginalFilename());
        System.err.println(files[0].getOriginalFilename());
        System.err.println(mfile.getOriginalFilename());
        String gid = backendGoodsService.addGoods(data, files, itemimg, mfile, attribute, account);
        return new ResponseResult<String>(SUCCESS,gid);
    }

    @RequestMapping("data")
    public ResponseResult<HashMap> getGoods(String y, String account) {

        HashMap data = backendGoodsService.GetGoodsAll(Integer.parseInt(y), account);

        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @RequestMapping("type")
    public ResponseResult<List<Dimdef>> getType(String account) {

        List<Dimdef> data = backendGoodsService.GetDimdef(account);
        return new ResponseResult<List<Dimdef>>(SUCCESS, data);
    }

    @RequestMapping("smalltype")
    public ResponseResult<List<Dimdef>> getSmallType(String id, String account) {

        List<Dimdef> data = backendGoodsService.GetSmallDimdef(id, account);
        return new ResponseResult<List<Dimdef>>(SUCCESS, data);
    }

    /**
     * 根据大分类小分类查询商品
     *
     * @param start
     * @param id
     * @param value
     * @return
     */
    @RequestMapping("classification")
    public ResponseResult<HashMap> queryClassification(Integer start, String id, String value, String account) {

        HashMap data = backendGoodsService.QueryClassification(start, id, value, account);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }


}
